//
//  CopyTest.m
//  iOS Sample Code
//
//  Created by lei yu on 11-12-11.
//  Copyright (c) 2011年 lancet. All rights reserved.
//

#import "CopyTest.h"


@implementation  Fraction
@synthesize numerator=numerator_,denumerator=denumerator_;

-(id)copyWithZone:(NSZone *)zone{
    Fraction *newFraction = [[Fraction allocWithZone:zone]init];
    newFraction.numerator=numerator_;
    newFraction.denumerator=denumerator_;
    
    return newFraction;
}

-(id)setTo:(int)to over:(int) over{
    numerator_=to;
    denumerator_=over;
    return self;
}


-(void)print{
    
    NSLog(@"The Fraction contains:[%d numerator,%d denumerator ]",numerator_,denumerator_);
    
}

@end


@implementation CopyTest

// All code under test must be linked into the Unit Test bundle
- (void)testMultableCopy
{
    NSAutoreleasePool * pool=[[NSAutoreleasePool alloc] init];
    NSMutableArray *dataArray= [NSMutableArray arrayWithObjects:@"one", @"two", @"three", @"four", nil];
    NSMutableArray *dataArray2;
    // simple assignment
    dataArray2=dataArray;
    [dataArray2 removeObjectAtIndex: 0];
    NSLog (@"dataArray: ");
    
    
    for ( NSString *elem in dataArray ){
        NSLog (@" %@", elem);
    }
    
    NSLog(@"dataArray2: ");
    
    
    for ( NSString *elem in dataArray2 ){
        // try a copy
        NSLog (@"%@", elem);
    }
    
    dataArray2= [dataArray mutableCopy];
    [dataArray2 removeObjectAtIndex: 0];
    
    NSLog (@"dataArray: ");
    
    for ( NSString *elem in dataArray ){ 
        NSLog (@" %@", elem);
    }
    
    NSLog (@"dataArray2: ");
    for ( NSString *elem in dataArray2 ){ 
        NSLog (@" %@", elem);
    }
    [dataArray2 release]; [pool drain];
    
    
}


-(void)testCopyingProtocol{
    NSAutoreleasePool * pool=[[NSAutoreleasePool alloc] init];
    Fraction *f1=[[Fraction alloc] init];
    Fraction *f2;
    [f1 setTo: 2 over: 5];
    f2=[f1 copy];
   
    [f2 setTo: 1 over: 3];
    [f1 print];
    [f2 print];
    [f1 release]; [f2 release]; [pool drain];
}

@end
